package com.itheima.studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {

        ArrayList<Student> list = new ArrayList<>();

        loop: while (true) {
            System.out.println("-----------------欢迎来到黑马学生管理系统----------------");
            System.out.println("1: 添加学生");
            System.out.println("2: 删除学生");
            System.out.println("3: 修改学生");
            System.out.println("4: 查询学生");
            System.out.println("5: 退出");
            System.out.println("请输入您的选择:");
            Scanner scanner = new Scanner(System.in);
            String choose = scanner.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> getStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    // break loop; // 跳出指定的循环
                    System.exit(0); // 停止虚拟机
                }
                default -> System.out.println("没有当前选项");
            }
        }

    }

    // 添加学生
    public static void addStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生id：");
        String id = scanner.next();
        // 检查当前id是否存在
        boolean result = containers(list, id);
        if(result) {
            System.out.println("当前学生已经添加");
            return;
        }
        System.out.println("请输入学生姓名：");
        String  name = scanner.next();

        System.out.println("请输入学生年龄：");
        int age = scanner.nextInt();

        System.out.println("请输入学生家庭住址：");
        String address = scanner.next();

        list.add(new Student(id, name, age, address));
    }

    // 删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生id：");
        String id = scanner.next();
        int index = getIndex(list, id);
        if(index >= 0) {
            list.remove(index);
            System.out.println("id为" + id + "删除成功");
        } else {
            System.out.println("id不存在，删除失败");
        }
    }

    // 修改学生
    public static void updateStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要修改的id");
        String id = scanner.next();
        int index = getIndex(list, id);
        if(index == -1) {
            System.out.println("输入的id" + id + "不存在，返回主菜单");
        }

        Student student = list.get(index);

        System.out.println("请输入需要修改学生的姓名");
        String newName = scanner.next();
        student.setName(newName);

        System.out.println("请输入需要修改学生的年龄");
        int newAge = scanner.nextInt();
        student.setAge(newAge);

        System.out.println("请输入需要修改学生的地址");
        String newAdress = scanner.next();
        student.setAddress(newAdress);

        System.out.println("修改成功");
    }

    // 查询学生
    public static void getStudent(ArrayList<Student> list) {
        if(list.isEmpty()) {
            System.out.println("当前无学生信息，请添加之后在查询");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (Student student : list) {
            System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getAddress());
        }
    }

    // 检查id是否存在
    public static boolean containers(ArrayList<Student> list, String id) {
        return  getIndex(list, id) >= 0;
    }

    // 根据id查询索引
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String suId = stu.getId();
            if(suId.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
